package cate.game.train.awakenfairy;

import cate.common.table.d.GDOperation;
import cate.common.table.item.awakenfairy.row.AwakenFairyBaseRow;
import cate.common.util.GameResult;
import cate.game.client.msg.MergeMsgSender;
import cate.game.event.RoleEventPublisher;
import cate.game.event.hero.HeroPowerChangeEvent;
import cate.game.res.CostSelect;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.res.RewardShow;
import cate.game.role.Role;
import cate.game.role.bag.awakenfairy.AwakenFairy;
import cate.game.role.bag.hero.Hero;
import cate.game.train.awakenfairy.msg.AwakenFairyUpdateResp;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class TrainAwakenFairyHandler implements RoleEventPublisher {
    public GameResult<Void> levelUp(
            Role role, String awakenFairyUid, String heroUid, byte pos, CostSelect cs) {
        GameResult<Void> r = new GameResult<>();
        AwakenFairy awakenFairy = getAwakenFairy(role, awakenFairyUid, heroUid, pos);
        if (awakenFairy == null) {
            return r.fail("觉醒灵器不存在");
        }
        AwakenFairyBaseRow row = awakenFairy.getTpl(role.getGame().fight.getPlayBuildContext());
        if (row == null) {
            return r.fail("觉醒灵器配置有误");
        }
        if (StringUtils.isBlank(row.levelUpCostStr) && StringUtils.isBlank(row.levelUpItemCostStr)) {
            return r.fail("觉醒灵器已满级");
        }
        MixRes levelUpConsume = new MixRes();
        if (StringUtils.isNotBlank(row.levelUpCostStr)) {
            levelUpConsume.addList(new MixRes(row.levelUpCostStr));
        }
        if (StringUtils.isNotBlank(row.levelUpItemCostStr)) {
            levelUpConsume.addList(new MixRes(row.levelUpItemCostStr));
        }
        GameResult<MixResItem> itemConsumeRes = levelUpConsume.consume(role, cs);
        if (!itemConsumeRes.ok()) {
            role.getGame().notice.tipResItem(role, itemConsumeRes.data);
            return r.fail(itemConsumeRes.message);
        }
        awakenFairy.tid = row.nextTid;
        role.sendNow(new AwakenFairyUpdateResp(awakenFairy, heroUid, pos));

        if (StringUtils.isNotBlank(heroUid)) {
            role.getBase().updatePower();
            publishEvent(role, new HeroPowerChangeEvent(role,  getHero(role, heroUid)));
        }
        role.getGame().notice.message(role, "升级成功");
        return r.success();
    }

    public GameResult<Void> back(Role role, String awakenFairyUid, String heroUid, byte pos) {
        GameResult<Void> r = new GameResult<>();
        AwakenFairy awakenFairy = getAwakenFairy(role, awakenFairyUid, heroUid, pos);
        if (awakenFairy == null) {
            return r.fail("觉醒灵器不存在");
        }
        AwakenFairyBaseRow row = awakenFairy.getTpl(role.getGame());
        if (row == null) {
            return r.fail("觉醒灵器配置有误");
        }
        MixRes backReward = new MixRes(row.backRwdStr);
        if (backReward.isEmpty()) {
            return r.fail("该觉醒灵器无法回炉");
        }
        row = role.getGame().table.awakenFairy.base.getRow(row.protoId, 0);
        if (row == null) {
            return r.fail("该觉醒灵器无法回炉");
        }
        GameResult<MixResItem> consumeRes = new MixRes(row.backCostStr).consume(role, null);
        if (!consumeRes.ok()) {
            role.getGame().notice.tipResItem(role, consumeRes.data);
            return r.fail(consumeRes);
        }
        awakenFairy.tid = row.id;
        role.sendNow(new AwakenFairyUpdateResp(awakenFairy, heroUid, pos));

        backReward.add(role, true, GDOperation.AWAKEN_FAIRY_BACK);
        RewardShow rewardShow = new RewardShow();
        rewardShow.addTplRwd(backReward);
        role.getGame().notice.dialog(role, rewardShow);

        if (StringUtils.isNotBlank(heroUid)) {
            Hero hero = getHero(role, heroUid);
            role.getBase().updatePower();
            publishEvent(role, new HeroPowerChangeEvent(role, hero));
        }
        role.getGame().notice.message(role, "回炉成功");
        return r.success();
    }

    public GameResult<Void> decomposed(Role role, List<String> awakenFairyUids) {
        GameResult<Void> r = new GameResult<>();
        if(awakenFairyUids == null || awakenFairyUids.isEmpty()){
            return r.fail("觉醒灵器不可为空");
        }
        MixRes reward = new MixRes();
        MergeMsgSender sender = new MergeMsgSender();
        for(String awakenFairyUid : awakenFairyUids){
            AwakenFairy awakenFairy = role.getBag().getAwakenFairy().getItem(awakenFairyUid);
            if (awakenFairy == null) {
                role.getGame().notice.message(role, "觉醒灵器不存在");
                continue;
            }
            AwakenFairyBaseRow fairyBaseRow = awakenFairy.getTpl(role.getGame());
            if (fairyBaseRow == null) {
                role.getGame().notice.message(role, "觉醒灵器不存在");
                continue;
            }
            MixRes treward = new MixRes();
            List<AwakenFairyBaseRow> rows = role.getGame().table.awakenFairy.base.getLevelLowerRows(fairyBaseRow.protoId, fairyBaseRow.level);
            if (!rows.isEmpty()) {
                for (AwakenFairyBaseRow row : rows) {
                    if (StringUtils.isNotBlank(row.returnStr)) {
                        treward.addList(new MixRes(row.returnStr));
                    }
                    if (row.level > 0 && StringUtils.isNotBlank(row.levelUpCostStr)) {
                        treward.addList(new MixRes(row.levelUpCostStr));
                    }
                    if (row.level > 0 && StringUtils.isNotBlank(row.levelUpItemCostStr)) {
                        treward.addList(new MixRes(row.levelUpItemCostStr));
                    }
                }
            }
            r = treward.addCheck(role);
            if (!r.ok()) {
                role.getGame().notice.message(role, r.message);
                continue;
            }
            GameResult<RewardShow> removeRes = role.getBag().getAwakenFairy().removeItem(awakenFairyUid, sender);
            // 发奖励
            if (!removeRes.ok()) {
                role.getGame().notice.message(role, removeRes.message);
                continue;
            }
            reward.addList(treward);
        }
        sender.send(role, true);
        reward.add(role,true,GDOperation.FAIRY_DECOMPOSE);
        role.getGame().notice.dialogReward(role, reward);
        return r;
    }

    private Hero getHero(Role role, String heroUid){
        return role.getBag().hero.getItem(heroUid);
    }

    /** 获取灵器对象，有英雄id从英雄身上取，没有则从背包里取 */
    public AwakenFairy getAwakenFairy(Role role, String awakenFairyUid, String heroUid, byte pos) {
        if (StringUtils.isNotBlank(heroUid)) {
            Hero hero = getHero(role, heroUid);
            if(hero != null){
                AwakenFairy awakenFairy = hero.awakenFairiesOn.awakenFairyMap.get(pos);
                if (StringUtils.equals(awakenFairyUid, awakenFairy.uid)) {
                    return awakenFairy;
                }
            }
        } else {
            return role.getBag().getAwakenFairy().getItem(awakenFairyUid);
        }
        return null;
    }
}
